package com.cooper.java.scraper;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: cooperlee
 * Date: 11/15/12
 * Time: 9:31 AM
 */
public class Exp {

    /*
public Exp(XmlPullParser xml) {
    repeat = Scraper
            .parseBool(xml.getAttributeValue(null, "repeat"), false);
    String nocleanStr = xml.getAttributeValue(null, "noclean");
    if (nocleanStr != null && !nocleanStr.trim().equals("")) {
        for (String s : nocleanStr.split(",")) {
            noclean.add(Integer.parseInt(s));
        }
    }

    String trimStr = xml.getAttributeValue(null, "trim");
    if (trimStr != null && !trimStr.trim().equals("")) {
        for (String s : trimStr.split(",")) {
            trim.add(Integer.parseInt(s));
        }
    }

    clear = Scraper.parseBool(xml.getAttributeValue(null, "clear"), false);
}    */

    public Exp(String repeat, String nocleanStr, String trimStr, String clear) {
        this.repeat = Scraper
                .parseBool(repeat, false);
        if (nocleanStr != null && !nocleanStr.trim().equals("")) {
            for (String s : nocleanStr.split(",")) {
                noclean.add(Integer.parseInt(s));
            }
        }
        if (trimStr != null && !trimStr.trim().equals("")) {
            for (String s : trimStr.split(",")) {
                trim.add(Integer.parseInt(s));
            }
        }
        this.clear = Scraper.parseBool(clear, false);
    }

    public boolean clear = false;
    private boolean repeat = false;
    private List<Integer> noclean = new ArrayList<Integer>();
    private List<Integer> trim = new ArrayList<Integer>();
    String rule;

    public String matchExp(String input, String output) {
        String result = "";
        List<String> rules = new ArrayList<String>();
        if ((rule == null) || rule.trim().equals("")) {
            rules.add(input);
            result = replaceOutput(rules, output);
        } else {
            Pattern p = Pattern.compile(rule);
            Matcher m = p.matcher(input);
            List<String> regGroups = new ArrayList<String>();
            boolean found = false;
            while (m.find()) {
                found = true;
                regGroups.clear();
                for (int i = 1; i <= m.groupCount(); i++) {
                    regGroups.add(m.group(i) == null ? "" : m.group(i));
                }
                result += replaceOutput(regGroups, output);
                if (!repeat)
                    break;
            }
            if (!found) {
                return null;
            }
        }
        return result;
    }

    String clean(String str) {
        return str.replaceAll("</{0,1}.*?>", "").replaceAll("&.*?;", "")
                .replace("&", "&amp;").replace("<", "&lt;")
                .replace(">", "&gt;").replace("'", "&apos;")
                .replace("\"", "&quot;").replace("&nbsp", " ");
    }

    private String replaceOutput(List<String> expGroupResults, String output) {
        String result = output;

        for (int i = expGroupResults.size() - 1; i >= 0; i--) {
            String egResults = expGroupResults.get(i);
            egResults = egResults == null ? "" : egResults;
            if (!noclean.contains(new Integer(i + 1))) {
                egResults = clean(egResults);
            }
            if (trim.contains(new Integer(i + 1))) {
                egResults = egResults.trim();
            }
            result = result.replace("\\\\" + String.valueOf(i + 1), egResults);
        }
        return result;
    }

}
